package website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.constants.Code;
import website.domain.*;
import website.mapper.LinkMapper;
import website.mapper.UserMapper;
import website.service.LinkService;
import website.service.WebService;
import website.utils.NowDateUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private LinkMapper linkMapper;

    @Autowired
    private WebService webService;

    /**
     * 发布友链
     * @param link
     * @return
     */
    @Override
    public ResModel issue(Link link) {
        if (link.getUid()!=null && link.getTagName()!=null
                && link.getLinkDesc()!=null && link.getLinkAddress()!=null){
            link.setIssueTime(NowDateUtils.getNowDate());
            boolean save = save(link);
            //更新网站留言总量
            webService.update().setSql("link_count = link_count + 1").update();
            return ResModel.success(Code.INSERT,save);
        }
        return ResModel.error(Code.UN_INSERT);
    }

    /**
     * 查询友链
     * @return
     */
    @Override
    public ResModel<List<Link>> getList() {
        List<Link> linkList = list();
        for (Link link : linkList) {
            Integer uid = link.getUid();
            User user = userMapper.getByUid(uid);
            link.setNickname(user.getNickname());
            link.setAvatar(user.getAvatar());
        }
        //更新网站浏览量
        webService.update().setSql("visits = visits + 1").update();
        return ResModel.success(Code.SUCCESS,linkList);
    }

    /**
     * 分页查询友链
     * @param current
     * @param size
     * @return
     */
    @Override
    public ResModel<IPage<Link>> getByPage(Integer current, Integer size) {
        Page<Link> page = new Page<>(current, size);
        Page<Link> messagePage = linkMapper.selectPage(page, null);
        return ResModel.success(Code.SUCCESS,messagePage);
    }

    /**
     * 删除友链
     * @param id
     * @return
     */
    @Override
    public ResModel delLink(Integer id) {
        if (id!=null){
            boolean b = removeById(id);
            //更新网站留言总量
            webService.update().setSql("link_count = link_count - 1").update();
            return ResModel.success(Code.DELETE,b);
        }
        return ResModel.error(Code.FAIL);
    }

    /**
     * 查询自己友链
     * @param uid
     * @return
     */
    @Override
    public ResModel<List<Link>> listByUid(Integer uid) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,Link::getUid,uid);
        List<Link> list = list(wrapper);
        return ResModel.success(Code.SUCCESS,list);
    }
}
